package cn.lagou.spark.dataChange

import org.apache.spark.SparkContext
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

object DataChange {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .appName(this.getClass.getCanonicalName)
      .master("local[*]")
      .getOrCreate()
    val sc: SparkContext = spark.sparkContext
    sc.setLogLevel("warn")

    val lst = List((1, "2019-03-04", "2020-02-03"), (2, "2020-04-05", "2020-08-04"), (3, "2019-10-09", "2020-06-11"))
    val df: DataFrame = spark.createDataFrame(lst).toDF("id", "startDate", "endDate")

    val dataSet: Dataset[Row] = df.select("startDate").union(df.select("endDate"))
    dataSet.createOrReplaceTempView("t1")

    spark.sql(
      """
        | select tmp.startDate, nvl( lead(tmp.startDate) over(partition by col order by tmp.startDate), startDate) endDate from
        | (select  "1"  col, startDate from t1) tmp
        |   """.stripMargin)
      .show
    spark.close()
  }
}
